package com.app.backend.activiti.serviceImpl;

import com.app.backend.activiti.dao.ActNodeDao;
import com.app.backend.activiti.entity.ActNode;
import com.app.backend.activiti.service.ActNodeService;
import com.app.backend.base.dao.RoleDao;
import com.app.backend.base.dao.UserDao;
import com.app.backend.base.entity.Role;
import com.app.backend.base.entity.User;
import com.app.backend.common.base.BaseDao;
import com.app.backend.common.constant.ActConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: ActNodeServiceImpl
 * @description:
 * @author: Zhun.Xiao
 * @create: 2020-03-01 10:17
 **/
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class ActNodeServiceImpl implements ActNodeService {

    @Autowired
    private ActNodeDao actNodeDao;


    @Autowired
    private RoleDao roleDao;

    @Autowired
    private UserDao userDao;

    @Override
    public BaseDao<ActNode, String> getRepository() {
        return actNodeDao;
    }

    @Override
    public void deleteByNodeId(String nodeId) {
        actNodeDao.deleteByNodeId(nodeId);
    }

    @Override
    public List<User> findUserByNodeId(String id) {
        List<ActNode> list = actNodeDao.findByNodeIdAndType(id, ActConstant.NODE_BIND_USER);
        List<User> users = new ArrayList<>();
        for (ActNode actNode : list) {
            User u = userDao.findById(actNode.getRelateId()).orElse(null);

            if (u != null) {
                users.add(u);
            }
        }

        return users;
    }

    @Override
    public List<Role> findRoleByNodeId(String id) {
        List<ActNode> list = actNodeDao.findByNodeIdAndType(id, ActConstant.NODE_BIND_ROLE);
        List<Role> roles = new ArrayList<>();
        for (ActNode actNode : list) {
            Role r = roleDao.findById(actNode.getRelateId()).orElse(null);
            if (r != null) {
                roles.add(r);
            }
        }
        return roles;
    }

}